package com.product.android.ui.widget;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 显示图片的上部分，只有getScaleType() == ScaleType.MATRIX时有效
 * 
 * @author Administrator
 * 
 */
public class TopCropImageView extends ImageView {

	public TopCropImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setScaleType(ScaleType.MATRIX);
	}

	public TopCropImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setScaleType(ScaleType.MATRIX);
	}

	public TopCropImageView(Context context) {
		super(context);
		setScaleType(ScaleType.MATRIX);
	}

	@Override
	protected boolean setFrame(int l, int t, int r, int b) {

		if (getDrawable() != null && getScaleType() == ScaleType.MATRIX) {
			Matrix matrix = getImageMatrix();
			float scaleWidth = (r - l) / (float) getDrawable().getIntrinsicWidth();
			matrix.setScale(scaleWidth, scaleWidth, 0, 0);
			setImageMatrix(matrix);
		}

		return super.setFrame(l, t, r, b);
	}
}
